//
//  BaseModel.m
//  XiaoHongShu
//
//  Created by Jason on 2022/6/18.
//

#import "BaseModel.h"

@implementation BaseModel

- (NSString *) description {
    //判断是否是NSArray 或者NSDictionary NSNumber 如果是的话直接返回 debugDescription
    if ([self isKindOfClass:[NSArray class]] || [self isKindOfClass:[NSDictionary class]] || [self isKindOfClass:[NSString class]] || [self isKindOfClass:[NSNumber class]]) {
        return [self debugDescription];
    }
    //声明一个字典
    NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
    //得到当前class的所有属性
    uint count;
    objc_property_t *properties = class_copyPropertyList([self class], &count);
    
    //循环并用KVC得到每个属性的值
    for (int i = 0; i<count; i++) {
        objc_property_t property = properties[i];
        //获取属性名
        NSString *name = @(property_getName(property));
        //通过 KVC 获取value
        id value = [self valueForKey:name] ? : @"nil";//默认值为nil字符串
        [dictionary setObject:value forKey:name];//装载到字典里
    }
    
    //释放
    free(properties);
    
    NSArray *keys = [dictionary allKeys];
    NSMutableString *resStr = [NSMutableString new];
    [resStr appendString:@"{ "];
    for (NSString *key in keys) {
        if ([key isEqualToString:keys[keys.count - 1]]) {
            [resStr appendFormat:@"%@=%@", key, [dictionary valueForKey:key]];
        } else {
            [resStr appendFormat:@"%@=%@, ", key, [dictionary valueForKey:key]];
        }
    }
    [resStr appendString:@" }"];
    //NSLog(@"---**%@",[NSString stringWithFormat:@"%@: %@",[self class], resStr]);
    //return
    return [NSString stringWithFormat:@"%@: %@",[self class], resStr];
}

// 重写debugDescription, 而不是description
- (NSString *)debugDescription {
    //判断是否时NSArray 或者NSDictionary NSNumber 如果是的话直接返回 debugDescription
    if ([self isKindOfClass:[NSArray class]] || [self isKindOfClass:[NSDictionary class]] || [self isKindOfClass:[NSString class]] || [self isKindOfClass:[NSNumber class]]) {
        return [self debugDescription];
    }
    //声明一个字典
    NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
    //得到当前class的所有属性
    uint count;
    objc_property_t *properties = class_copyPropertyList([self class], &count);
    
    //循环并用KVC得到每个属性的值
    for (int i = 0; i<count; i++) {
        objc_property_t property = properties[i];
        NSString *name = @(property_getName(property));
        id value = [self valueForKey:name]?:@"nil";//默认值为nil字符串
        [dictionary setObject:value forKey:name];//装载到字典里
    }
    
    //释放
    free(properties);
    NSLog(@"&&& - %@",[NSString stringWithFormat:@"<%@: %p> -- %@",[self class],self,dictionary]);
    //return
    return [NSString stringWithFormat:@"<%@: %p> -- %@",[self class],self,dictionary];
}

//将字典转成功的模型添加到数组中
+ (NSArray<BaseModel *> *) arrayFromJSONArray:(NSArray *)jsonArray {
    NSMutableArray<BaseModel *> *baseModelArray = [NSMutableArray array];
    //遍历从网络上获取的数组
    for (NSDictionary *dict in jsonArray) {
        [baseModelArray addObject:[[self class] yy_modelWithDictionary:dict]];
    }
    return [baseModelArray mutableCopy];
}

@end
